using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
//
using SJBotGame.SJBotGameComponents;

namespace SJBotGame
{
    public class SJBotSheetDraw
    {
        private readonly Rectangle rect;
        private readonly SJBotSheet sheet;
		private readonly Graphics formGraphics;
        //
        private SJBotTitleDraw[,] sheetDraw;
        //
        public SJBotSheetDraw(SJBotSheet sheet, Rectangle rect, Graphics formGraphics)
        {
			this.formGraphics = formGraphics;
            this.sheet = sheet;
            this.rect = rect;
            //
            selectedTitle = null;
            //
            CreateSheetDraw();
        }
 		~SJBotSheetDraw()
		{
			formGraphics.Dispose();
		}
        //
        private void CreateSheetDraw()
        {
            sheetDraw = new SJBotTitleDraw[sheet.XSize, sheet.YSize];
            //
            int xRectSize = rect.Width / sheet.XSize;
            int yRectSize = rect.Height / sheet.YSize;
            for (int i = 0; i < sheet.XSize; i++)
            {
                for (int j = 0; j < sheet.YSize; j++)
                {
                    sheetDraw[i, j] = new SJBotTitleDraw(sheet.GetTitle(new Point(i, j)), new Rectangle(i * xRectSize, j * yRectSize, xRectSize - 2, yRectSize - 2));
                }
            }
        }
		private void UpdateSheetDraw()
        {
            for (int i = 0; i < sheet.XSize; i++)
            {
                for (int j = 0; j < sheet.YSize; j++)
                {
					if (sheetDraw[i, j].Title != sheet.GetTitle(new Point(i, j)))
					{
						UpdateTitleDraw(new Point(i, j));
					}
                }
            }
		}
        //


        // 
        private Point selectedTitle;
        public void SelectTitle(Point point)
        {
            for (int i = 0; i < sheet.XSize; i++)
            {
                for (int j = 0; j < sheet.YSize; j++)
                {
                    if (sheetDraw[i, j].Rect.Contains(point.X, point.Y))
                    {
                        Point _selectedTitle = new Point(i, j);
						if (sheet.GetTitle(_selectedTitle) == null)
						{
							return;
						}
                        //
                        if (selectedTitle == null)
                        {
                            selectedTitle = new Point(i, j);
                            HighlightTitle(selectedTitle);
                        }
                        else
                        {
                            if (!selectedTitle.Equals(_selectedTitle))
                            {
                                if (selectedTitle.IsNear(_selectedTitle))
                                {
                                    HighlightTitle(_selectedTitle);
                                    HighlightTitle(selectedTitle);
                                    HighlightTitle(_selectedTitle);
                                    //
                                    sheet.ChangeTitle(selectedTitle, _selectedTitle);
									UpdateSheetDraw();
                                    //
									selectedTitle = null;
                                }
                                else
                                {
                                    HighlightTitle(selectedTitle);
                                    HighlightTitle(_selectedTitle);
                                    selectedTitle = _selectedTitle;
                                }
                            }
                            else
                            {
                                HighlightTitle(selectedTitle);
                                selectedTitle = null;
                            }
                        }
                        break;
                    }
                }
            }
        }
        //
        private void UpdateTitleDraw(Point point)
        {
            sheetDraw[point.X, point.Y].Title = sheet.GetTitle(point);
            DrawTitle(sheetDraw[point.X, point.Y]);
        }
		//
        private void HighlightTitle(Point point)
        {
            HighlightTitle(point, -1);
        }
        private void HighlightTitle(Point point, int select)
        {
            SJBotTitleDraw titleDraw = sheetDraw[point.X, point.Y];
            if (select == -1)   titleDraw.Highlight = !titleDraw.Highlight;
            else                titleDraw.Highlight = Convert.ToBoolean(select);
            DrawTitle(titleDraw);
            sheetDraw[point.X, point.Y] = titleDraw;
        }
        //
        public void Draw()
        {
            foreach(SJBotTitleDraw titleDraw in sheetDraw)
            {
                DrawTitle(titleDraw);
            }
        }
        private void DrawTitle(SJBotTitleDraw titleDraw)
        {
            if (titleDraw != null) titleDraw.Draw(formGraphics);
        }
    }
}
